package cn.edu.hrbcu.es.invigilatemanagerment.controller;

import cn.edu.hrbcu.es.invigilatemanagerment.pojo.Role;
import cn.edu.hrbcu.es.invigilatemanagerment.pojo.Teacher;
import cn.edu.hrbcu.es.invigilatemanagerment.pojo.TeacherRole;
import cn.edu.hrbcu.es.invigilatemanagerment.service.IRoleService;
import cn.edu.hrbcu.es.invigilatemanagerment.service.ITeacherRoleService;
import cn.edu.hrbcu.es.invigilatemanagerment.service.ITeacherService;
import lombok.extern.java.Log;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.servlet.ModelAndView;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

@Controller
@Slf4j
@RequestMapping("/TeacherRole")
public class TeacherRoleController {
    @Autowired
    private ITeacherRoleService teacherRoleService;
    @Autowired
    private ITeacherService teacherService;
    @Autowired
    private IRoleService roleService;
    @RequestMapping("queryAllTeacherRole")
    public String queryAllTeacherRole(Model model,@RequestParam(value = "pageNow",defaultValue = "1")int pageNow,
                                      @RequestParam(value = "pageSize",defaultValue = "5")int pageSize){
        int totalCount = teacherRoleService.queryTeacherRoleCount();
        int pageCount = (int)Math.ceil((double)totalCount/pageSize);
        List<TeacherRole> teacherRoleList = teacherRoleService.queryTeacherRoleByPage(pageNow,pageSize);
        List<Map<String,Object>> nameList = new ArrayList<>();
        if(teacherRoleList!=null&&!teacherRoleList.isEmpty())
        {
            for(TeacherRole teacherRole:teacherRoleList)
            {
                Map<String,Object> nameMap = new HashMap<>();
                nameMap.put("id",teacherRole.getId());
                nameMap.put("teacherName",teacherService.queryTeacherById(teacherRole.getTeacherid()).getName());
                nameMap.put("roleName",roleService.queryRoleById(teacherRole.getRoleid()).getName());
                nameList.add(nameMap);
            }
        }
        model.addAttribute("teacherRoleList",nameList);
        model.addAttribute("pageCount",pageCount);
        model.addAttribute("pageNow",pageNow);
        model.addAttribute("pageSize",pageSize);
        return "teacherroles";
    }

    @RequestMapping("toAddTeacherRole")
    public String toAddTeacherRole(Model model)
    {
        List<Teacher> teachers = teacherService.queryAllTeacher();
        List<Role> roles = roleService.queryAllRole();
        model.addAttribute("teachers",teachers);
        model.addAttribute("roles",roles);
        return "toaddteacherrole";
    }

    @RequestMapping("toUpdateTeacherRole")
    public ModelAndView toUpdateTeacherRole(Integer id)
    {
        TeacherRole teacherRole = teacherRoleService.queryTeacherRolesById(id);
        List<Teacher> teachers = teacherService.queryAllTeacher();
        List<Role> roles = roleService.queryAllRole();
        ModelAndView model = new ModelAndView("toupdateteacherrole");
        model.addObject("teacherRole",teacherRole);
        model.addObject("teachers",teachers);
        model.addObject("roles",roles);
        return model;
    }

    @RequestMapping("updateTeacherRoleById")
    @ResponseBody
    public String updateTeacherRoleById(TeacherRole teacherRole)
    {
        teacherRoleService.updateTeacherRoleByPrimaryId(teacherRole);
        return "OK";
    }

    @RequestMapping("insertTeacherRole")
    @ResponseBody
    public String insertTeacherRole(TeacherRole teacherRole)
    {
        teacherRoleService.insertTeacherRole(teacherRole);
        return "OK";
    }

    @RequestMapping("deleteTeacherRoleById")
    @ResponseBody
    public String deleteTeacherRoleById(Integer id)
    {
        teacherRoleService.deleteTeacherRoleByPrimaryId(id);
        return "OK";
    }

}
